//
//  ZXTitleView.m
//  XiMaFM
//
//  Created by fanzhuxian on 2017/9/7.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "ZXTitleView.h"

#import "ZXTitleButton.h"
#import "UIButton+JFAdd.h"

@interface ZXTitleView ()

/** 三角箭头 **/
@property (nonatomic, strong) UIImageView * arrowImgView;

/** 更多按钮 **/
@property (nonatomic, strong) UIButton * moreBtn;

@end

@implementation ZXTitleView

- (instancetype)initWithTitle:(NSString *)title hasMore:(BOOL)more {
    if (self = [super init]) {
        
        self.title = title;
        self.arrowImgView.image = [UIImage imageNamed:@"tabbar_np_play"];
        [self.titleLabel setTextColor:[UIColor blackColor]];
        
        if (more) {
            [self moreBtn];
        }
        
        self.backgroundColor = [UIColor whiteColor];
    }
    
    return self;
}

#pragma mark - 懒加载
- (UIImageView *)arrowImgView {

    if (!_arrowImgView) {
        _arrowImgView = [[UIImageView alloc] init];
        [self addSubview:_arrowImgView];
        [_arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(8);
            make.bottom.mas_equalTo(-15);
            make.size.mas_equalTo(CGSizeMake(10, 10));
        }];
    }
    return _arrowImgView;
}

- (UILabel *)titleLabel {

    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.arrowImgView);
            make.left.mas_equalTo(self.arrowImgView.mas_right).mas_equalTo(4);
            make.width.mas_equalTo(150);
        }];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.text = _title;
    }
    return _titleLabel;
}

- (UIButton *)moreBtn {

    if (!_moreBtn) {
        _moreBtn = [[ZXTitleButton alloc] init];
        [self addSubview:_moreBtn];
        [_moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-5);
//            make.centerY.mas_equalTo(self.titleLabel);
            make.bottom.mas_equalTo(-15);
            make.size.mas_equalTo(CGSizeMake(60, 20));
        }];
        
        [_moreBtn setTitle:@"更多" forState:UIControlStateNormal];
        [_moreBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_moreBtn setImage:[UIImage imageNamed:@"cell_arrow"] forState:UIControlStateNormal];
        [_moreBtn jf_layoutButtonWithEdgeInsetsStyle:JF_ButtonEdgeInsetsStyleRight imageTitleSpace:10];
        
        // 按钮点击事件
        [_moreBtn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    }
    return _moreBtn;
}

- (void)click {

    if ([self.delegate respondsToSelector:@selector(titleViewDidClick:)]) {
        [self.delegate titleViewDidClick:self.tag];
    }
    NSLog(@"更多按钮被点击 %ld", self.tag);
}

@end
